/*!
 Temelia - Hash Set samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/hash_set.h>
#include <math.h>

static int hash(void *x, void *context)
{
	return sqrt((*(int *) x));
}

static void sample1()
{
	int **x, N = 10, i, result;
	hash_set_t set;

	PRINT("\n\n Sample 1 started \n\n");

	set = hash_set_new(N);
	x = int_array_new(N * N, N * N);

	for (i = 0; i < N * N; i++)
	{
		result = hash_set_put(set, x[i], hash, NULL, int_compare, NULL);
		if (result == 1)
			PRINT("Added ");
		else
			PRINT("Already exists ");
		PRINT("%d\n", x[i][0]);
	}

	PRINT("Printing the hash set ...\n");
	hash_set_iterate(set, int_handler, NULL);

	PRINT("Empty %d\n", hash_set_is_empty(set));
	PRINT("Size %d\n", hash_set_get_size(set));
	PRINT("%d %d\n",
			hash_set_contains(set, x[5], hash, NULL, int_compare, NULL),
			hash_set_contains(set, NULL, NULL, NULL, NULL, NULL));

	PRINT("Remove first half of keys ...\n");
	for (i = 0; i < N * N / 2; i++)
		hash_set_remove(set, x[i], hash, NULL, int_compare, NULL);
	hash_set_iterate(set, int_handler, NULL);
	PRINT("Size %d\n", hash_set_get_size(set));

	hash_set_clear(set);
	PRINT("Printing the empty hash set ...\n");
	hash_set_iterate(set, int_handler, NULL);

	for (i = 0; i < N * N; i++)
		free(x[i]);
	free(x);
	hash_set_delete(set);

	PRINT("\n\n Sample 1 ended \n\n");
}

static void sample2()
{
	hash_set_t set;
	int i, N = 100;

	PRINT("\n\n Sample 2 started \n\n");

	set = hash_set_new(sqrt(N));
	for (i = 0; i < N; i++)
		hash_set_put(set, int_new(i), hash, NULL, int_compare, NULL);
	hash_set_iterate(set, int_handler, NULL);
	hash_set_iterate(set, int_delete, NULL);
	hash_set_delete(set);

	PRINT("\n\n Sample 2 ended \n\n");
}

void run_hash_set_samples()
{
	sample1();
	sample2();
}
